package com.shopping.dao.provider;

import com.shopping.pojo.Receive;
import com.shopping.pojo.User;
import javafx.scene.control.Tab;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;

public class UpdateSelectiveProvider extends SQL{
    private static final String TABLE_NAME01 = "user";
    private static final String TABLE_NAME02 = "receive";

    public String userUpdateSelective(final User user){
        return new SQL(){{
            UPDATE(TABLE_NAME01);
            if(user.getAvatar()!=null){
                SET("avatar = #{avatar}");
            }
            if(!StringUtils.isEmpty(user.getNickName())){
                SET("nick_name = #{nickName}");
            }
            WHERE("id = #{id}" );
        }}.toString();
    }
    public String receiveUpdateSelective(final Receive receive){
        return new SQL(){{
            UPDATE(TABLE_NAME02);
            if(!StringUtils.isEmpty(receive.getReceiveName())){
                SET("receive_name = #{receiveName}");
            }
            if(!StringUtils.isEmpty(receive.getReceiveAddress())){
                SET("receive_address = #{receiveAddress}");
            }
            if(!StringUtils.isEmpty(receive.getPostcode())){
                SET("postcode = #{postcode}");
            }
            if(!StringUtils.isEmpty(receive.getPhone())){
                SET("phone = #{phone}");
            }
            if(receive.getUserId()!=0){
                SET("user_id = #{userId}");
            }
        }}.toString();
    }
}
